package com.tsu.mall.admin.controller;


import com.tsu.mall.admin.entity.Waresclassify;
import com.tsu.mall.admin.service.IWaresclassifyService;
import com.tsu.mall.common.VO.LayuiTableVO;
import com.tsu.mall.common.VO.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tttttt
 * @since 2023-03-17
 */
@Controller
@RequestMapping("/waresclassify")
public class WaresclassifyController {

    @Autowired
    private IWaresclassifyService iWaresclassifyService;
    @GetMapping("/waresclassifyPage")
    public String classifyPage(){
        return "classify";
    }

    //上传图片功能
    @PostMapping("/avatar")
    @ResponseBody
    public LayuiTableVO avatar(MultipartFile file){
        String fileName=System.currentTimeMillis()+".jpg";
        File f=new File("D:/test/"+fileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        LayuiTableVO vo=new LayuiTableVO();

        String s="/upload/"+fileName;
        Map<String ,String> map=new HashMap<>();
        map.put("imgPath",s);
        vo.setCode(0);
        vo.setData(map);
        return vo;
    }

    @GetMapping("/list")
    @ResponseBody
    public LayuiTableVO list(String keyword){
        return  iWaresclassifyService.doList(keyword);
    }

    @PostMapping("/del")
    @ResponseBody
    public ResultVO delete(Integer id) {
        return iWaresclassifyService.doDelete(id);
    }

    @GetMapping("/{id}")
    @ResponseBody
    public Waresclassify selectById(@PathVariable Integer id){
        return iWaresclassifyService.getById(id);
    }

    @PostMapping("/save")
    @ResponseBody
    public void save(Waresclassify classify){
        iWaresclassifyService.saveOrUpdate(classify);
    }

}
